<?php
/**
 * 系统设置
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @author cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */

class OptionController extends UE_Controller_Admin {
	/**
	 * OptionModel
	 *
	 * @var OptionModel
	 */
	private $_option;
	public function init() {
		UE_Loader::loadModel('OptionModel', $this->app);
		$this->_option = new OptionModel();
	}
	public function listAction() {
		$sql = "SELECT * FROM ue_option_type WHERE option_type_enabled = 1 ORDER BY option_type_order";
		$optionTypes = UE_DB::factory()->getAll($sql);

		$wh = ' WHERE o.option_autoload=1 AND ot.option_type_enabled = 1';
		$wh .= $this->id ? " AND o.option_type='{$this->id}'" : '';
		
		$totals = UE_DB::factory()->getfirstField("SELECT COUNT(*) FROM ue_option o LEFT JOIN ue_option_type ot ON o.option_type = ot.option_type $wh");
		$paging = new UE_Paging();
		$paging->setVar($totals, 15);
		
		$sql = "SELECT * FROM ue_option o
		        LEFT JOIN ue_option_type ot ON o.option_type = ot.option_type
		        $wh 
		        ORDER BY ot.option_type_order, o.option_order 
		        LIMIT {$paging->firstCount}, {$paging->pageNo}";
		$options = UE_DB::factory()->getAll($sql);
		
		$paging = $paging->pagingStr;
	    include $this->tpl();
	}
	
	/**
	 * 批量修改系统设置(系统选项设置)
	 */
	public function settingAction() {
		if (IS_POST) {
			foreach ($_POST as $k=>$v) {
				$this->_option->writeOptionValue($k, $v);
			}
			$this->setOK('成功修改配置');
		}
		
		$typeGroup = empty($_GET['type_group']) ? 0 : (int)$_GET['type_group'];
		$optionTyps = $this->_option->getOptionTypes($typeGroup, false);
		
		$typeTmp = array();
		foreach ($optionTyps as $t) {
			$typeTmp[] = $t['option_type'];
		}
		
		$sql = "SELECT * FROM ue_option WHERE option_type IN('".implode("','", $typeTmp)."') AND option_autoload=1 ORDER BY option_type, option_order";
		$settings = UE_DB::factory()->getAll($sql);
		//print_r($settings);
		
		include $this->tpl(); 
	}
	
	/**
	 * 修改系统选项输入类型
	 *
	 */
	public function setAttrAction() {
		if (empty($this->id)) {
			print 'error param';
			return ;
		}
		
		$this->_option->setId($this->id);
		$optionTyps = $this->_option->getOptionTypes();
		
		if (IS_POST) {
			if ($this->_option->update($_POST)) {
				$this->setOK('成功修改配置属性');
				$this->_option->setId($_POST['option_name']);
			} else {
				$this->setErr($this->_option->getErrs());
			}
		}
		
		$optionAttr = $this->_option->read();
		//print_r($optionAttr);
		
		include $this->tpl();
	}
}